<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
date_default_timezone_set ( 'prc' );
class Kickback extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回商品列表
	 */
	public function index() {
		$id = $this->uri->segment(4);
		$name = $this->uri->segment(6);
		$name=urldecode($name);//转化为汉字
		if(empty($name)){
    		$name="empty";
    	}
		$this->load->library ( 'pagination' );
		// 每页显示数据条数
		$per_page=20;//暂定为20条
		
		$config['base_url']=site_url('kickback/index/id/'.$id.'/name/'.$name);
		$this->load->model ( 'kickback_model' );
		$this->load->model ( 'user_model' );
		if($name=="empty"){//这里是为了搜索
    		$name=null;
    	}
		// 数据总数
		$config ['total_rows'] = $this->kickback_model->count_kickbacks($id,$name);
		//$config ['total_rows']=200; 
		$config ['per_page'] = $per_page;
		$config ['first_link'] = '第一页';
		$config ['last_link'] = '最后一页';
		$config ['next_link'] = '下一页';
		$config ['prev_link'] = '上一页';
		$config['uri_segment'] =7;
		
		$this->pagination->initialize($config);
		$data['links']=$this->pagination->create_links();
		//偏移量
		$offset=intval($this->uri->segment(7));
		$data ['kickbacks'] = $this->kickback_model->find_kickbacks ($id,$name,$per_page,$offset);
		//$data ['userid'] =$id;
		$data ['user']=$this->user_model->load($id);
		$data ['title'] = '商品列表 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加商品页面
	 */
	public function add() {
		$id = $this->uri->segment(4);
		$data ['userid'] =$id;
		$data ['title'] = '添加商品 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑商品页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'kickback_model' );
		$data ['kickback'] = $this->kickback_model->load ( $id );
		$data ['title'] = '编辑商品 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存或者更新商品的信息
	 */
	public function save() {
		$adminid = $this->session->userdata ( 'id' );
		$green = $this->input->post('green');
		$id = $this->input->post('id');
		$productid = $this->input->post('productid');
		$this->load->model ( 'user_model' );
		$this->load->model ( 'kickback_model' );
		if(empty($productid)){
			return_json(FALSE,'','商品ID不能为空！');
		}
		if($green==1){
			$users = $this->user_model->find_green_users ();
			foreach($users as $user){
				$temp=$this->kickback_model->find_by_user_product($user['id'],$productid);
				if(!empty($temp)){//删掉原来的信息
					$this->kickback_model->delete ( $temp['id'] );
				}
				$kickback = array (
					'productid' => $productid,
					'userid' => $user['id'],
					'rate' => $this->input->post ( 'rate' ),
					'newprice' => $this->input->post ( 'newprice' ),
					'addprice' => $this->input->post ( 'addprice' ),
					'admin' => $adminid,
					'date'=>date ( "Y-m-d H:i:s" )
				);
				$this->kickback_model->create ( $kickback);
			}
			if($id){
				return_json(TRUE,'','更新成功！');
			}else{
				return_json(TRUE,'','添加成功！'); 
			}
		}else{
			
		$kickback = array (
				'productid' => $productid,
				'userid' => $this->input->post ( 'userid' ),
				'rate' => $this->input->post ( 'rate' ),
				'newprice' => $this->input->post ( 'newprice' ),
				'addprice' => $this->input->post ( 'addprice' ),
				'admin' => $adminid,
				'date'=>date ( "Y-m-d H:i:s" )
		);
		if ($id) {
			if($this->kickback_model->update ( $id, $kickback )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','信息未改动！');
			}
		} else {
			$kickbackid=$this->kickback_model->create ( $kickback );
			if(!empty($kickbackid)){
				return_json(TRUE,'','添加成功！'); 
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
		
		}
	}
	/*
	 *一键添加信息
	 */
	public function oneclick() {
	   $this->load->model ( 'product_model' );
	   $products = $this->product_model->find_products ();
	   $this->load->model ( 'kickback_model' );
	   $i=1;
	   foreach ( $products as $product ) {
	   		if($i>260){
	   			break;
	   		}
			$kickback = array (
				'productid' => $product['id'],
				'userid' => 1134,
				'addprice' => 0.2
			);
			$this->kickback_model->create ( $kickback );
			$i++;
	   }
	   return_json(TRUE,'','添加成功！'); 
	}
	/*
	 *一键添加信息
	 */
	public function oneclick2() {
	   $this->load->model ( 'product_model' );
	   $products = $this->product_model->find_products ();
	   $this->load->model ( 'kickback_model' );
	   foreach ( $products as $product ) {
	   		if($product['categoryid']==17||$product['categoryid']==20){
	   			$kickback = array (
					'productid' => $product['id'],
					'userid' => 1046,
					'addprice' => 1
				);
				$this->kickback_model->create ( $kickback );
	   		}
	   }
	   return_json(TRUE,'','添加成功！'); 
	}
	/*
	 * 删除商品信息
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'kickback_model' );
		if ($this->kickback_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
		}else{
			return_json(TRUE,'','删除失败！');
		}
	}
	/*
	 * 返回每月下单金额
	 */
	public function order() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'user_model' );
		$data ['user']=$this->user_model->load ( $id );
		$this->load->model ( 'order_model' );
		$data ['orders'] = $this->order_model->get_month_total_by_userid ( $id );
		$data ['title'] = '查看每月下单金额 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/order' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回当月每天下单金额
	 */
	public function detail() {
		$id = $this->uri->segment(4);
		$date = $this->uri->segment(6);
		$this->load->model ( 'user_model' );
		$data ['user']=$this->user_model->load ( $id );
		$this->load->model ( 'order_model' );
		$data ['orders'] = $this->order_model->get_day_total_by_userid ( $id,$date);
		$data ['title'] = '查看当月每天下单金额 - 回扣管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'kickback/detail' );
		$this->load->view ( 'footer' );
	}
}